<template>
	<div>
		<el-breadcrumb separator-class="el-icon-arrow-right">
			<el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
			<el-breadcrumb-item>收废品</el-breadcrumb-item>
			<el-breadcrumb-item>门店管理</el-breadcrumb-item>
		</el-breadcrumb>
		<el-link :underline="false" style="font-size: 30px;">全部门店</el-link>&nbsp&nbsp
		<el-link :underline="false" style="font-size: 30px;">上架中</el-link>&nbsp&nbsp
		<el-link :underline="false" style="font-size: 30px;">已下架</el-link>
		<el-form :inline="true">
			<el-form-item label="门店名称:">
				<el-input v-model="queryInfo.shopName"></el-input>
			</el-form-item>
			<el-form-item label="门店手机号:">
				<el-input v-model="queryInfo.phone"></el-input>
			</el-form-item>
			<el-form-item label="提交日期:">
				<el-date-picker value-format="yyyy-MM-dd hh:mm:ss" v-model="dataValue" type="daterange" align="right"
					unlink-panels range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期"
					:picker-options="pickerOptions">
				</el-date-picker>
			</el-form-item>
			<el-form-item>
				<el-button @click="getinfo" type="primary">搜索</el-button>
			</el-form-item><br />


			<el-form-item>
				<el-button :title="title" @click="add" type="primary"> 新增门店</el-button>
				<el-dialog :title="title" :visible.sync="dialogFormVisible" @close="cancel">
					<el-form :model="shopData">
						<el-form-item label="门店名称" label-width="80px" style="margin-top: 10px;">
							<el-input v-model="shopData.shopName"></el-input>
						</el-form-item>
						<el-form-item label="门店手机号" label-width="80px" style="margin-top: 10px;">
							<el-input v-model="shopData.shopPhone"></el-input>
						</el-form-item>
						<el-form-item label="所属地区" label-width="80px" style="margin-top: 10px;">
							<el-cascader v-model="address" :options="citys" @change="handleChange"></el-cascader>
						</el-form-item>
						<el-form-item label-width="80px" style="margin-top: 10px;">
							<el-input placeholder="详细地址" v-model="shopData.shopAddress"></el-input>
						</el-form-item>
						<el-form-item label="门店坐标" label-width="80px" style="margin-top: 10px;">
							<el-button @click="getLocation">获取位置</el-button>
							<!--放置经纬度信息-->
							<span v-show="show">{{shopData.shopLatitude}},{{shopData.shopLongitude}}</span>
						</el-form-item>
						<el-form-item label="营业时间" label-width="80px" style="margin-top: 10px;">
							<el-input v-model="shopData.shopDescribe"></el-input>
						</el-form-item>
					</el-form>
					<div slot="footer" class="dialog-footer">
						<el-button @click="cancel">取消</el-button>
						<el-button type="primary" @click="updateCommunity">确 定</el-button>
					</div>
				</el-dialog>
			</el-form-item>


		</el-form>
		<el-table :header-cell-style="{fontSize: '14px', backgroundColor: '#1890FF',color:'#ffffff'}" :data="tableData"
			border style="width: 100%">
			<el-table-column prop="shopName" label="门店" width="180">
			</el-table-column>
			<el-table-column prop="shopId" label="ID" width="180">
			</el-table-column>
			<el-table-column prop="shopPhone" label="门店手机号" width="180">
			</el-table-column>
			<el-table-column prop="shopAddress" label="门店地址" width="180">
			</el-table-column>
			<el-table-column prop="shopDescribe" label="营业时间" width="180">
			</el-table-column>
			<el-table-column prop="createTime" label="关联小区" width="180">
			</el-table-column>
			<el-table-column prop="shopOrderCount" label="订单总数" width="180">
			</el-table-column>
			<el-table-column prop="communityShopCount" label="提交方式" width="180">
			</el-table-column>
			<el-table-column label="状态"><el-link :underline="false" type="primary">上架中</el-link>
			</el-table-column>
			<el-table-column fixed="right" label="操作" width="100">
				<template slot-scope="scope">
					<el-button @click="handleClick" type="text" size="small">查看关联门店</el-button>
					<el-button @click="ed(scope.row)" type="text" size="small">编辑门店</el-button>
					<!-- <el-button  @click="open">点击打开 Message Box</el-button> -->

					<!-- 	<el-button @click="del(scope.row.communityId)" type="text" size="small">删除</el-button> -->
				</template>
			</el-table-column>
		</el-table>
		<el-row style="margin-top: 10px;">
			<div class="block">
				<el-pagination layout="prev, pager, next,jumper,total,sizes" @size-change="handleSizeChange"
					@current-change="handleCurrentChange" :current-page="queryInfo.pageNum" :page-sizes="[1,5,10,20]"
					:page-size="queryInfo.pageSize" :total="total">
				</el-pagination>
			</div>
		</el-row>
	</div>
</template>

<script>
	import citys from '../assets/citydata.js';
	export default {
		data() {
			return {
				show: false,
				title: '新增门店',
				address: [],
				shopData: {
					shopAddress: "",
					shopAreaCode: "",
					shopCity: "",
					shopCoordinates: "",
					shopDistrict: "",
					shopId: "",
					shopLatitude: "",
					shopLongitude: "",
					shopName: "",
					shopProvince: "",
				},
				citys: citys,
				dialogFormVisible: false,

				total: 0,
				tableData: [],
				queryInfo: {
					pageNum: 1,
					pageSize: 10,
					phone: '',
					keyword: '',
					shopStatus: '',
					startTime: '',
					endTime: '',

				},
				dataValue: [],
				input: '',
				pickerOptions: {
					shortcuts: [{
						text: '最近一周',
						onClick(picker) {
							const end = new Date();
							const start = new Date();
							start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
							picker.$emit('pick', [start, end]);
						}
					}, {
						text: '最近一个月',
						onClick(picker) {
							const end = new Date();
							const start = new Date();
							start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
							picker.$emit('pick', [start, end]);
						}
					}, {
						text: '最近三个月',
						onClick(picker) {
							const end = new Date();
							const start = new Date();
							start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
							picker.$emit('pick', [start, end]);
						}
					}]
				},
				value1: '',
				value2: ''
			}
		},
		methods: {
			open() {

			},
			cancel() {
				this.dialogFormVisible = false;
				this.address = [];
				this.shopData = {
					shopAddress: "",
					shopAreaCode: "",
					shopCity: "",
					shopCoordinates: "",
					shopDistrict: "",
					shopId: "",
					shopLatitude: "",
					shopLongitude: "",
					shopName: "",
					shopProvince: "",
				};


			},
			updateCommunity() {
				if (this.title == "新增门店") {
					this.$http.post('shop/addShop', this.shopData)
						.then((res) => {
							if (res.data.status == 200) {
								this.$message.success('添加成功');
								this.dialogFormVisible = false;
								this.getinfo();
							} else {
								this.$message.error('操作失败');
							}
						})
						.catch(error => {
							console.error(error);
							this.$message.error('请求失败，请检查输入信息');
						});
				} else if (this.title == "编辑门店") {
					this.$http.post('shop/modifyShop', this.shopData)
						.then((res) => {
							if (res.data.status == 200) {
								this.$message.success('编辑成功');
								this.dialogFormVisible = false;
								this.getinfo();
							} else {
								this.$message.error('操作失败');
							}
						})
						.catch(error => {
							console.error(error);
							this.$message.error('请求失败，请检查输入信息');
						});
				}

			},
			handleChange() {
				console.log('');
			},
			getLocation() {
				if (!this.shopData.shopDistrict) {
					this.$message.warning('请选择所属地区')
					return
				}
				this.show = true;
				const address = this.shopData.shopProvince +
					this.shopData.shopCity +
					this.shopData.shopDistrict +
					this.shopData.shopAddress;

				this.$http.get('https://restapi.amap.com/v3/geocode/geo', {
					params: {
						key: '457886b5972a97e93fb9cdccebfd450f',
						address: address
					}
				}).then((resp) => {
					if (resp.data.status == '1' && resp.data.geocodes.length > 0) {
						let location = resp.data.geocodes[0].location;
						this.shopData.shopLatitude = location.split(',')[0];
						this.shopData.shopLongitude = location.split(',')[1];
					} else {
						this.$message.error('地址转换失败，请检查地址是否正确');
					}
				}).catch(error => {
					console.error('API 请求错误:', error.response ? error.response.data : error.message);
					this.$message.error('获取位置信息失败，请检查控制台错误信息');
				});
			},
			add() {
				this.dialogFormVisible = true;
			},
			handleClick() {
				console.log('查看关联门店');
			},
			ed(row) {
				this.title = "编辑门店";
				this.dialogFormVisible = true;
				this.shopData = row;
				this.address = [
					this.shopData.shopProvince,
					this.shopData.shopCity,
					this.shopData.shopDistrict,
					this.shopData.shopAddress
				]
			},
			// 	del(shopId) {
			// 		this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
			// 		  confirmButtonText: '确定',
			// 		  cancelButtonText: '取消',
			// 		  type: 'warning'
			// 		}).then(() => {
			// 		  this.$message({
			// 		    type: 'success',
			// 		    message: '删除成功!'
			// 		  });
			// 		}).catch(() => {
			// 		  this.$message({
			// 		    type: 'info',
			// 		    message: '已取消删除'
			// 		  });          
			// 		}).then(
			// 		this.$http.post('community/communityRemove',{'communityId':communityId
			// })
			// 		.then((res)=>{
			// 			if(res.data.status==200){
			// 				this.getinfo();
			// 			}
			// 		}))
			// 	},
			handleSizeChange(val) {
				this.queryInfo.pageSize = val;
				this.queryInfo.pageNum = 1;
				this.getinfo();
			},
			handleCurrentChange(val) {
				this.queryInfo.pageNum = val;
				this.getinfo();
			},
			getinfo() {
				this.queryInfo.keyword = this.queryInfo.shopName;
				this.$http.get('shop/querySearchShopList', {
						params: this.queryInfo
					})
					.then((res) => {
						this.tableData = res.data.data.list;
						this.total = res.data.data.total;
						if (res.data.status == 200) {
							console.log(res.data.data.list)
						} else {
							this.$message.error('服务器接口异常')
						}
					})
			}
		},
		mounted() {
			this.getinfo();
		},
		watch: {
			address() {
				this.shopData.shopProvince = this.address[0];
				this.shopData.shopCity = this.address[1];
				this.shopData.shopDistrict = this.address[2];
			},
			dataValue() {
				if (this.dataValue) {
					this.queryInfo.startTime = this.dataValue[0];
					this.queryInfo.endTime = this.dataValue[1]
				} else {
					this.queryInfo.startTime = '',
						this.queryInfo.endTime = ''
				}
			}
		}
	}
</script>

<style>
</style>